import request from '@/utils/request'

export function getRoutes(roleId) {
  return request({
    url: `/routerResource/currentRoleResources?roleId=${roleId}`,
    method: 'get',
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

export function getRouteIds(roleId) {
  return request({
    url: `/routerResource/currentRoleResourceIds?roleId=${roleId}`,
    method: 'get',
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

export function getAllRoles() {
  return request({
    url: '/role/allRoles',
    method: 'get',
    headers: {
      'Content-Type': 'x-www-form-urlencoded'
    }
  })
}

export function addRole(data) {
  return request({
    url: '/role/addRole',
    method: 'post',
    data
  })
}

export function updateRole(data) {
  return request({
    url: `/role/updateRole`,
    method: 'post',
    data
  })
}

export function deleteRole(roleId) {
  return request({
    url: `/role/delRole/${roleId}`,
    method: 'delete'
  })
}

export function getRolesByUid(uid) {
  return request({
    url: `/role/getUserRoles?uid=${uid}`,
    method: 'get'
  })
}

/**
 * 查找用户未分配角色
 * @param {用户ID} uid
 * @returns Promise
 */
export function getUserNotAllocateRoles(uid) {
  return request({
    url: `/role/userNotAllocateRoles?uid=${uid}`,
    method: 'get'
  })
}

/**
 * 给用户添加角色
 * @param {要授权的角色ID集合} roleIds
 * @param {用户ID} uid
 * @returns Promise
 */
export function addRolesForUser(roleIds, uid) {
  return request({
    url: `/role/addRolesForUser?uid=${uid}`,
    method: 'post',
    data: roleIds
  })
}

/**
 * 删除用户角色
 * @param {要解除授权的角色ID集合} roleIds
 * @param {用户ID} uid
 * @returns Promise
 */
export function delRolesForUser(roleIds, uid) {
  return request({
    url: `/role/userRoles?uid=${uid}`,
    method: 'delete',
    data: roleIds
  })
}

export function getAllRouterIds() {
  return request({
    url: `/routerResource/allRouterIds`,
    method: 'get'
  })
}

export function addRouteIdsForRole(routeIds, roleId) {
  return request({
    url: `/routerResource/addRouteIds?roleId=${roleId}`,
    method: 'post',
    data: routeIds
  })
}
